package com.hengpeng.itfin.persist.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.stereotype.Repository;

import com.hengpeng.common.persist.AbstractDao;
import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Restrictions;
import com.hengpeng.common.util.RandomImageGenerator;
import com.hengpeng.common.util.SignatureHelper;
import com.hengpeng.common.util.Validate;
import com.hengpeng.itfin.persist.VerifyCodeDao;
import com.hengpeng.itfinbase.persist.VerifyCode;
import com.hengpeng.itfinbase.persist.VerifyCode.VerifyType;

/**
 * <pre>
 * 校验码DAO
 * </pre>
 * @author zhaojitao
 * @version 1.0, 2010-12-5
 */
@Repository
public class VerifyCodeDaoImpl extends AbstractDao<VerifyCode> implements VerifyCodeDao
{

	@Override
	public VerifyCode create(String userName, VerifyType type, Date pastDate)
	{
		// 使该类型以前的校验码失效
		List<VerifyCode> list = find(userName, type);
		if (list != null && list.size() > 0)
			for (VerifyCode bean : list)
			{
				if (bean.getValid().equals(BoolValue.YES))
				{
					bean.setValid(BoolValue.NO);
					update(bean);
				}
			}
		// 创建新的校验码实体
		VerifyCode bean = new VerifyCode();
		bean.setType(type);
		bean.setUserName(userName);
		bean.setCreateDate(new Date());
		bean.setPastDate(pastDate);
		bean.setValid(BoolValue.YES);
		bean.setValue(SignatureHelper.generateMD5Signture(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
				+ RandomImageGenerator.random(20), "utf-8"));
		return save(bean);
	}

	@Override
	public VerifyCode createForSMS(String userName, VerifyType type, Date pastDate, String mobile)
	{
		// 使该类型以前的校验码失效
		List<VerifyCode> list = find(userName, type);
		if (list != null && list.size() > 0)
			for (VerifyCode bean : list)
			{
				if (bean.getValid().equals(BoolValue.YES))
				{
					bean.setValid(BoolValue.NO);
					update(bean);
				}
			}
		// 创建新的校验码实体
		VerifyCode bean = new VerifyCode();
		bean.setType(type);
		bean.setUserName(userName);
		bean.setCreateDate(new Date());
		bean.setPastDate(pastDate);
		bean.setValid(BoolValue.YES);
		bean.setMobile(mobile);
		Random random = new Random();
		StringBuffer sBuff = new StringBuffer();
		for (int i = 0; i < 4; i++)
		{
			sBuff.append(random.nextInt(10));
		}
		bean.setValue(sBuff.toString());
		return save(bean);
	}

	@Override
	public List<VerifyCode> find(String userName, VerifyType type)
	{
		if (Validate.validateObjectsNullOrEmpty(userName, type))
		{
			return null;
		}
		DynamicQuery query = new DynamicQuery(VerifyCode.class);
		query.add(Restrictions.eq("userName", userName));
		query.add(Restrictions.eq("type", type));
		return super.findAllByDynamicQuery(query);
	}

	@Override
	public VerifyCode get(String userName, VerifyType type, String value)
	{
		if (Validate.validateObjectsNullOrEmpty(userName, type, value))
		{
			return null;
		}
		DynamicQuery query = new DynamicQuery(VerifyCode.class);
		query.add(Restrictions.eq("userName", userName));
		query.add(Restrictions.eq("type", type));
		query.add(Restrictions.eq("value", value));
		return super.findAllByDynamicQuery(query).get(0);
	}

}
